package voidgame.core;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class NavigatePanel extends JPanel{
	
	Image background;
	Game game;
	int range;
	int[] inspectPos;
	int[] centerPoint;
	
	public NavigatePanel(Game currentGame){
		
		System.out.println("NAV UP "+currentGame.getClass());
		game=currentGame;
		ImageIcon ii = new ImageIcon("C:\\eclipse\\workspace\\Void\\images\\starscape.jpg");
        background = ii.getImage();
               
        this.setForeground(Color.GRAY);
        
        range = 5;
        
        centerPoint = new int[2];
        
        inspectPos = new int[2];
        inspectPos[0]=0;
        inspectPos[1]=0;

	}
	
	public void paint(Graphics g) {

		//System.out.println("TEST PAINT");
		
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(background, 0, 0, null);
        
        int dimensions = (range*2)+1;
        
        
        centerPoint[0]=game.player.pos[0];
        centerPoint[1]=game.player.pos[1];
        
        if(centerPoint[0]-range<0)
        	centerPoint[0]=range;
        if(centerPoint[0]+range>(game.MAPSIZEX-1))
        	centerPoint[0]=(game.MAPSIZEX-1)-range;
        if(centerPoint[1]-range<0)
        	centerPoint[1]=range;
        if(centerPoint[1]+range>(game.MAPSIZEY-1))
        	centerPoint[1]=(game.MAPSIZEY-1)-range;
        
        int cellXsize = this.getWidth()/dimensions;
        int cellYsize = this.getHeight()/dimensions;
        
        g2d.setColor(Color.GRAY);
        
        for(int x=0;x<dimensions;x++){
        	for(int y=0;y<dimensions;y++){
        		g2d.drawRect(x*cellXsize, y*cellYsize, cellXsize, cellYsize);
        		Cell cell = game.player.starSystem.map[centerPoint[0]+(x-range)][centerPoint[1]+(y-range)];
        		if(cell.type!=0){
        			//System.out.println("TEST "+cell.obj.sprite+" "+cellXsize+"*"+cell.obj.spriteSize+"="+new Double(Math.floor(cellXsize*cell.obj.spriteSize)).intValue()+" "+new Double(Math.floor(cellYsize*cell.obj.spriteSize)).intValue());
        			
        			g2d.drawImage(
        					new ImageIcon(cell.obj.sprite).getImage(), 
        					new Double(x*cellXsize+((cellXsize/2)-((cellXsize/2)*cell.obj.spriteSize))).intValue(), 
        					new Double(y*cellYsize+((cellYsize/2)-((cellYsize/2)*cell.obj.spriteSize))).intValue(), 
        					new Double(Math.round(cellXsize*cell.obj.spriteSize)).intValue(), 
        					new Double(Math.round(cellYsize*cell.obj.spriteSize)).intValue(), 
        					null
        			);
        			
        			if(cell.type==2){
        				Planet p = (Planet)cell.obj;
        				if(p.rings==1&&!p.system.name.equals("Sol")){
        					g2d.drawImage(
                					new ImageIcon(p.ringSprite).getImage(), 
                					new Double(x*cellXsize+((cellXsize/2)-((cellXsize/2)*cell.obj.spriteSize))).intValue(), 
                					new Double(y*cellYsize+((cellYsize/2)-((cellYsize/2)*cell.obj.spriteSize))).intValue(), 
                					new Double(Math.round(cellXsize*cell.obj.spriteSize)).intValue(), 
                					new Double(Math.round(cellYsize*cell.obj.spriteSize)).intValue(), 
                					null
                			);
        				}
        				if(p.rings==2&&!p.system.name.equals("Sol")){
        					g2d.drawImage(
                					new ImageIcon(p.ringSprite).getImage(), 
                					new Double(x*cellXsize+((cellXsize/2)-((cellXsize/2)*cell.obj.spriteSize))).intValue(), 
                					new Double(y*cellYsize+((cellYsize/2)-((cellYsize/2)*cell.obj.spriteSize))).intValue(), 
                					new Double(Math.round(cellXsize*cell.obj.spriteSize)).intValue(), 
                					new Double(Math.round(cellYsize*cell.obj.spriteSize)).intValue(), 
                					null
                			);
        				}
        			}
        		}
        		
        		if(game.player.pos[0]==(centerPoint[0]+(x-range))&&game.player.pos[1]==(centerPoint[1]+(y-range))){
        			
        			g2d.translate(
        					new Double(x*cellXsize+((cellXsize/2))).intValue(), 
        					new Double(y*cellYsize+((cellYsize/2))).intValue()
        			);
        			
        			g2d.rotate(Math.toRadians(90*(game.player.orientation-1)));
        			
        			g2d.drawImage(
        					new ImageIcon(game.player.ship.sprite).getImage(), 
        					new Double(-((cellXsize/2)*game.player.ship.spriteSize)).intValue(), 
        					new Double(-((cellYsize/2)*game.player.ship.spriteSize)).intValue(), 
        					new Double(Math.round(cellXsize*game.player.ship.spriteSize)).intValue(), 
        					new Double(Math.round(cellYsize*game.player.ship.spriteSize)).intValue(), 
        					null
        			);
        			
        			g2d.rotate(Math.toRadians(-90*(game.player.orientation-1)));
        			
        			g2d.translate(
        					-new Double(x*cellXsize+((cellXsize/2))).intValue(), 
        					-new Double(y*cellYsize+((cellYsize/2))).intValue()
        			);
        			
        			/*g2d.drawImage(
        					new ImageIcon(game.player.ship.sprite).getImage(), 
        					new Double(x*cellXsize+((cellXsize/2)-((cellXsize/2)*game.player.ship.spriteSize))).intValue(), 
        					new Double(y*cellYsize+((cellYsize/2)-((cellYsize/2)*game.player.ship.spriteSize))).intValue(), 
        					new Double(Math.round(cellXsize*game.player.ship.spriteSize)).intValue(), 
        					new Double(Math.round(cellYsize*game.player.ship.spriteSize)).intValue(), 
        					null
        			);*/      			
        		}
        		
        		if(game.gameMode==Game.GAMEMODEINSPECT&&(x-5)==inspectPos[0]&&(y-5)==inspectPos[1]){
        			g2d.setColor(Color.RED);
        			g2d.drawRect(x*cellXsize+1, y*cellYsize+1, cellXsize-2, cellYsize-2);
        			g2d.setColor(Color.GRAY);
        		}
        			
        	}
        }
    }
	
	public void moveInspect(int dir){
		switch(dir){
			case 0:{
				inspectPos[0]--;
				break;
			}
			case 1:{
				inspectPos[1]--;
				break;
			}
			case 2:{
				inspectPos[0]++;
				break;
			}
			case 3:{
				inspectPos[1]++;
				break;
			}
		}
		
		if(inspectPos[0]<-range)
			inspectPos[0]=-range;
		if(inspectPos[0]>range)
			inspectPos[0]=range;
		if(inspectPos[1]<-range)
			inspectPos[1]=-range;
		if(inspectPos[1]>range)
			inspectPos[1]=range;
		
	}
	
}